import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    group: '',
    signature: '',
    roles: {
      menus: [],
      points: []
    }

  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  // 删除缓存
  removeToken(state) {
    state.token = null// 删除缓存
    removeToken()
  },
  // 设置用户信息
  setUserInfo(state, userInfo) {
    state.userInfo = { ...userInfo }// 使用浅拷贝赋值对象
  },
  // 删除用户信息
  removeUserInfo(state) {
    state.userInfo = {}
  }
}

const actions = {
  // 用户登录
  login({ commit }, userInfo) {
    const username = userInfo.username.trim()
    return new Promise((resolve, reject) => {
      login({
        username: username,
        password: userInfo.password
      }).then(response => {
        const { data } = response
        // 这里需要调整，读取token的深浅不一致
        // 触发mutations
        // console.log(data.token)
        commit('SET_TOKEN', data.token)
        // 存储到本地
        setToken(data.token)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // async login(context, data) {
  //   // 调用api接口
  //   const result = await login(data) // 拿到token
  //   context.commit('SET_TOKEN', result) // 设置token
  //   // 拿到token说明登录成功
  //   // setTimeStamp() // 设置当前的时间戳
  // },

  // 获取用户资料
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        // console.log(response)
        // console.log(111)

        if (!data) {
          return reject('Verification failed, please Login again.')
        }
        // 读取的字段可能不一致
        const { name, avatar, roles } = data
        // 设置给state
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_ROLES', roles)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // async getInfo(context) {
  //   const result = await getInfo()
  //   // 获取用户的详情 用户的详情数据
  //   // const baseInfo = await getUserDetailById(result.userId)
  //   context.commit('setUserInfo', { ...result }) // 提交到mutations
  //   return result // 这里为什么要return呢 这里是给我们后期做权限的时候 留下的伏笔
  // },

  // 退出操作
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        // 移除token，先移除token
        removeToken()
        // 重置路由
        resetRouter()
        // 重置state数据
        commit('RESET_STATE')
        commit('permission/setRoutes', [], { root: true })
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  }

  // 移除token
  // resetToken({ commit }) {
  //   return new Promise(resolve => {
  //     // 先移除token
  //     removeToken()
  //     // 重置state数据
  //     commit('RESET_STATE')
  //     resolve()
  //   })
  // }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

